package com.appspot.autopartymanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slim3.datastore.Datastore;

import com.appspot.autopartymanager.model.GlobalValue;

public enum GlobalValues {

    external_api_url_hotpepper("http://webservice.recruit.co.jp/hotpepper/gourmet/v1/?key=1138c2df6d63f81d");
    
    private final String value;
    
    private GlobalValues(String defaultValue) {
        String value = _GlobalValues.MAP.get(this.name());
        if (value != null) {
            this.value = value;
        } else {
            this.value = defaultValue;
        }
    }
    
    public String getValue() {
        return value;
    }
}

final class _GlobalValues {
    private static final Logger logger = LoggerFactory.getLogger(_GlobalValues.class);
    
    static final Map<String, String> MAP = new HashMap<String, String>();

    static {
        try {
            List<GlobalValue> globalValues = Datastore.get(GlobalValue.class);
            for (GlobalValue globalValue : globalValues) {
                MAP.put(globalValue.getKey().getName(), globalValue.getValue());
            }            
        } catch (Throwable e) {
            // ignore
            logger.debug("ignore this error.", e);
        }

    }
}
